<?php  
class AttributeController extends AuthController{
	private $db;
	private $cat_id;
	public function __init(){
		$this->db=K('Attribute');
		$this->cat_id=Q('cat_id',0,'intval');
		if($this->cat_id)$this->assign('cat_id',$this->cat_id);
	}
	public function index(){
		$this->cat_id?$map['cat_id']=array('EQ',$this->cat_id):$map='';
		$data=$this->db->where($map)->all();
		if (!empty($data)) {
			foreach ($data as $k=>$d) {	
				if (!empty($d['attr_value'])) {
					$dd=unserialize($d['attr_value']);
					$c='';
					foreach ($dd as $v) {
						$c.=$v;	
					}
					$data[$k]['attr_value']=$c;
				}
			}
		}
		int_to_string($data,array('attr_type'=>array(0=>'普通属性',1=>'规格属性')));
		int_to_string($data,array('attr_input_type'=>array(0=>'文本框',1=>'下拉列表框',2=>'文本域')));
		$this->assign('data',$data);
		$this->assign('goods_type',S('goods_type'));
		$this->display();
	}
	public function add(){
		if (IS_POST) {
			if ($this->db->addAttribute()) {
				$this->success('添加成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$this->assign('goods_type',S('goods_type'));
			$this->display();
		}
	}
	public function edit(){
		if (IS_POST) {
			if ($this->db->editAttribute()) {
				$this->success('编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$data=$this->db->where("attr_id=".Q('attr_id'))->find();
			if (!empty($data['attr_value'])) {
				$dd=unserialize($data['attr_value']);
				$c='';
				foreach ($dd as $v) {
					$c.=$v;	
				}
				$data['attr_value']=$c;
			}
			$this->assign('data',$data);
			$this->assign('goods_type',S('goods_type'));
			$this->display();
		}
	}
	public function del(){
		if ($this->db->delAttribute()) {
			$this->success('删除成功','index');
		}else{
			$this->error($this->db->error);
		}
	}
}
?>